package com.jiot.controller.user;

import java.security.Key;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.redis.Redis;
import com.jiot.common.constant.BusinessConstant;
import com.jiot.common.constant.CacheConstant;
import com.jiot.common.model.vo.UserVO;
import com.jiot.exce.EnumError;
import com.jiot.util.RSACoder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

public class LoginService {
	
	@Inject
	UserService userService; 

	// 发送手机验证码
	public Ret getPhoneCode(String phone) {
		//是否已经发送验证码
		String oldcode = CacheKit.get(CacheConstant.registerVCode, phone);
		if (StrUtil.isNotBlank(oldcode)) {
			return Ret.fail("error", EnumError.SMS_VCode_Sent.getMsg());
		}
		//是否已存在
		
		return Ret.ok();
	}
	
	/**
	 *  注册
	 * @param account
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public Ret register(String phone, String account, String code) throws Exception {

		//验证码
		String oldcode = CacheKit.get(CacheConstant.registerVCode, phone);
		if(!StrUtil.equals(oldcode, code)){
			return Ret.fail("error", EnumError.SMS_VCode_INVALID.getMsg());
		}
		//账号
		
		// 注册
		Map<String, Key> keyMap = RSACoder.initKey();
		String publicKey = RSACoder.getPublicKey(keyMap);
		String privateKey = RSACoder.getPrivateKey(keyMap);
		Random ran = new Random();
		String nickName = BusinessConstant.NICKNAME + (char) (0x4e00 + ran.nextInt(0x9fa5 - 0x4e00 + 1));
		Date date= new Date();
		
		return Ret.ok("publicKey",publicKey);
	}
	
	/**
	 *  注册2步
	 * @param account
	 * @param password 加密后的
	 * @return
	 * @throws Exception
	 */
	public Ret register2(String phone, String code, String password) throws Exception {
		//验证码
		String oldcode = CacheKit.get(CacheConstant.registerVCode, phone);
		if(!StrUtil.equals(oldcode, code)){
			return Ret.fail("error", EnumError.SMS_VCode_INVALID.getMsg());
		}
		
		byte[] decodedData = RSACoder.decryptByPrivateKey(password, "");
		String outputStr = new String(decodedData);
		
		return Ret.ok();
	}
	
	
	
	
	/**
	 * 更新/找回 密码，发送验证短信
	 * @param account
	 * @param oldPassword
	 * @param newPassword
	 * @throws Exception
	 */
	public Ret updatePasswordSendPhoneCode(String phone) {
		//是否已经发送验证码
		String oldcode = CacheKit.get(CacheConstant.forgetVCode, phone);
		if (StrUtil.isNotBlank(oldcode)) {
			return Ret.fail("error", EnumError.SMS_VCode_Sent.getMsg());
		}
		
		
//		String content = StrUtil.replace(PropKit.get("MessageContent"), "code", code, Boolean.TRUE);
//		JSONObject jsonParam = new JSONObject();
//		jsonParam.put("appid", PropKit.get("MessageAppId"));// 14674
//		jsonParam.put("to", phone);
//		jsonParam.put("content", content);
//		jsonParam.put("signature", PropKit.get("MessageSignature"));// c5c8fd9a4708a2e321d4d82b8d864b9f
//		HttpRequest.post(PropKit.get("MessageHttpPost")).header("content-type",BusinessConstant.RESPONSE_CONTENT_JSON).body(JSON.toJSONString( jsonParam)).execute();
		
		return Ret.ok();
	}
	
	
	/**
	 * 修改密码
	 * @param account
	 * @param password 加密后的
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public Ret updatePassword(String phone,String account,String code) throws Exception {
		String oldcode = CacheKit.get(CacheConstant.forgetVCode, phone);
		if(!StrUtil.equals(oldcode, code)){
			return Ret.fail("error", EnumError.SMS_VCode_INVALID.getMsg());
		}
		
		// 重签
		Map<String, Key> keyMap = RSACoder.initKey();
		String publicKey = RSACoder.getPublicKey(keyMap);
		String privateKey = RSACoder.getPrivateKey(keyMap);
		return Ret.ok("publicKey",publicKey);
	}
	
	public Ret updatePassword2(String phone, String code, String password) throws Exception {
		System.out.println(password);
		//验证码
		String oldcode = CacheKit.get(CacheConstant.forgetVCode, phone);
		if(!StrUtil.equals(oldcode, code)){
			return Ret.fail("error", EnumError.SMS_VCode_INVALID.getMsg());
		}
		
		byte[] decodedData = RSACoder.decryptByPrivateKey(password, "");
		String outputStr = new String(decodedData);
		UserVO userVO = JSONUtil.toBean("", UserVO.class);
        return Ret.ok("user", userVO);
	}
	
	
	
	
	
	public Ret login(String account,String password) throws Exception {
		
		
		byte[] decodedData = RSACoder.decryptByPrivateKey(password, "");
		String outputStr = new String(decodedData);
		
        UserVO userVO = JSONUtil.toBean("", UserVO.class);
        return Ret.ok("user", userVO);
	}
	
	public Ret logout(String account) {
		Redis.use(CacheConstant.appUser).del(account);
		return Ret.ok();
	}
	
	
}
